package 力扣._347前K高频元素;

import java.util.*;

class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int n : nums) {
            if (map.containsKey(n)) {
                map.put(n, map.get(n) + 1);
            } else {
                map.put(n, 1);
            }
        }
        List<int[]> t = new LinkedList<>();
        int j = 0;
        Set<Integer> set = map.keySet();
        for (int n : set) {
            t.add(new int[]{n, map.get(n)});
        }
        t.sort((o1, o2) -> o2[1] - o1[1]);
        int[] r = new int[k];
        for (int i = 0; i < k; i++)
            r[i] = t.get(i)[0];
        return r;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 1, 1, 2, 2, 3};
        int k = 2;
        int[] ints = new Solution().topKFrequent(nums, k);
        System.out.println(Arrays.toString(ints));
    }
}